package com.sea.finance.service.impl;


import com.codecrane.core.util.DateUtils;
import com.codecrane.core.web.BatchImportUtils;
import com.sea.finance.entity.Inventory;
import com.sea.finance.entity.PaymentAgain;
import com.sea.finance.entity.PaymentChange;
import com.sea.finance.mapper.PaymentAgainMapper;
import com.sea.finance.mapper.PaymentChangeMapper;
import com.sea.finance.service.InventoryService;
import com.sea.finance.service.PaymentAgainService;
import com.sea.finance.service.PaymentChangeService;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Date;
import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class PaymentChangeServiceImpl implements PaymentChangeService {

    @Autowired
    private PaymentChangeMapper paymentChangeMapper;
    @Autowired
    private InventoryService inventoryService;


    @Override
    public List<PaymentChange> findByCnd(PaymentChange paymentChange) {
        return paymentChangeMapper.findByCnd(paymentChange);
    }

    @Override
    public int save(PaymentChange paymentChange) {
        return paymentChangeMapper.insert(paymentChange);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importPaymentChange(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<PaymentChange> list = BatchImportUtils.iteratorForPaymentChange(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少新增一条明细！");
            } else {
                Date monthDate = DateUtils.addMonth(new Date(), -1);
                String lastMonth = DateUtils.dateToString(monthDate, "yyyy-MM");
                for (PaymentChange paymentChange : list) {
                    if(!lastMonth.equals(paymentChange.getPaymentDate())){
                        throw new RuntimeException("只能获取上月数据!");
                    }else{
                        Inventory inventory=new Inventory();
                        inventory.setInventoryChannelName(paymentChange.getPaymentBeforeChannelName());
                        inventory.setInventorySku(paymentChange.getPaymentSkuBefore());
                        inventory.setInventoryDate(paymentChange.getPaymentDate());
                        //查找修改前的SKU是否存在
                        Inventory before = inventoryService.findBySkuChannelDate(inventory);
                        if(null==before){
                            throw new Exception("暂未查询到改数据【SKU:'"+paymentChange.getPaymentSkuBefore()+"'】！");
                        }
                        //查找修改后的SKU是否存在
                        inventory.setInventoryChannelName(paymentChange.getPaymentAfterChannelName());
                        inventory.setInventorySku(paymentChange.getPaymentSkuAfter());
                        Inventory after = inventoryService.findBySkuChannelDate(inventory);
                        if(null==after){
                            throw new Exception("暂未查询到改数据【SKU:'"+paymentChange.getPaymentSkuAfter()+"'】！");
                        }
                        paymentChangeMapper.insert(paymentChange);

                        //修改经销存前SKU数量成本
                        Integer inventoryBeginNumber = before.getInventoryBeginNumber()-paymentChange.getPaymentNumber();
                        if(inventoryBeginNumber<0){
                            throw new Exception("期初数量不能为负数【SKU:'"+before.getInventorySku()+"'】！");
                        }
                        if(inventoryBeginNumber.equals(0)){

                            before.setInventoryChange(before.getInventoryChange()+paymentChange.getPaymentNumber());
                        }else {
                            before.setInventoryBeginNumber(inventoryBeginNumber);
                            before.setInventoryBeginMoney(before.getInventoryBeginNumber() * before.getInventoryBeginPrice());
                            before.setInventoryChange(before.getInventoryChange() + paymentChange.getPaymentNumber());
                        }

                        //修改经销存后SKU数量成本
                        Integer afterBeginNumber = after.getInventoryBeginNumber()+paymentChange.getPaymentNumber();

                        after.setInventoryBeginCustomFreight((paymentChange.getPaymentNumber()*before.getInventoryBeginCustomFreight()+after.getInventoryBeginNumber()*after.getInventoryBeginCustomFreight())/afterBeginNumber);
                        after.setInventoryBeginCustomTax((paymentChange.getPaymentNumber()*before.getInventoryBeginCustomTax()+after.getInventoryBeginNumber()*after.getInventoryBeginCustomTax())/afterBeginNumber);
                        after.setInventoryBeginUnitPrice((paymentChange.getPaymentNumber()*before.getInventoryBeginUnitPrice()+after.getInventoryBeginNumber()*after.getInventoryBeginUnitPrice())/afterBeginNumber);
                        after.setInventoryBeginPrice(after.getInventoryBeginCustomFreight()+after.getInventoryBeginCustomTax()+after.getInventoryBeginUnitPrice());

                        after.setInventoryBeginNumber(afterBeginNumber);
                        after.setInventoryBeginMoney(after.getInventoryBeginNumber()*after.getInventoryBeginPrice());
                        after.setInventoryChange(after.getInventoryChange()+paymentChange.getPaymentNumber());

                        inventoryService.usave(after);

                        if(inventoryBeginNumber.equals(0)){
                            before.setInventoryBeginNumber(0);
                            before.setInventoryBeginMoney(0.00);
                            before.setInventoryBeginCustomFreight(0.00);
                            before.setInventoryBeginUnitPrice(0.00);
                            before.setInventoryBeginCustomTax(0.00);
                            before.setInventoryBeginPrice(0.00);
                        }
                        inventoryService.usave(before);
                    }
                }
            }
        }
    }

    @Override
    public List<PaymentChange> findByMonth(String month) {
        return paymentChangeMapper.findByMonth(month);
    }
}
